﻿//===============================================================================
// Copyright (C) 2007-2010  HARBORHOUSEHOME   
// ALL RIGHTS RESERVED
//===============================================================================
// FILENAME : TradesSoldRequest
// DESCRIPTION :
// CREATED BY FETY AT   2010-5-14 15:11:17
//===============================================================================

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using TaobaoAPI.Util;

/// <summary>
///TradesSoldRequest 的摘要说明
/// </summary>
namespace TaobaoAPI.Request
{
    /// <summary>
    /// TOP API: taobao.trades.sold.get
    /// </summary>
    public class TradesSoldRequest:BaseRequest
    {
        public string BuyerNick { get; set; }
        public Nullable<DateTime> EndCreated { get; set; }
        public string Fields { get; set; }
        public Nullable<int> PageNo { get; set; }
        public Nullable<int> PageSize { get; set; }
        public string RateStatus { get; set; }
        public Nullable<DateTime> StartCreated { get; set; }
        public string Status { get; set; }
        public string Tag { get; set; }
        public string Type { get; set; }

        #region ITopRequest Members

        public override string GetApiName
        {
            get { return "taobao.trades.sold.get"; }
        }

        public IDictionary<string, string> GetParameters()
        {
            TopDictionary parameters = new TopDictionary();
            parameters.Add("buyer_nick", this.BuyerNick);
            parameters.Add("end_created", this.EndCreated);
            parameters.Add("fields", this.Fields);
            parameters.Add("page_no", this.PageNo);
            parameters.Add("page_size", this.PageSize);
            parameters.Add("rate_status", this.RateStatus);
            parameters.Add("start_created", this.StartCreated);
            parameters.Add("status", this.Status);
            parameters.Add("tag", this.Tag);
            parameters.Add("type", this.Type);
            return parameters;
        }

        public override string DoPost(string sessionKey)
        {
            Fields = "tid,seller_nick,buyer_nick,status,orders.title,orders.price,orders.num";
            PageSize = 40;
            // 添加协议级请求参数
            TopDictionary txtParams = new TopDictionary(GetParameters());
            txtParams.Add(METHOD, GetApiName);
            txtParams.Add(VERSION, "2.0");
            txtParams.Add(APP_KEY, appKey);
            txtParams.Add(FORMAT, format);
            txtParams.Add(PARTNER_ID, partnerId.ToString());
            txtParams.Add(TIMESTAMP, DateTime.Now);
            txtParams.Add(SESSION, sessionKey);

            // 添加签名参数
            txtParams.Add(SIGN, TopUtils.SignTopRequest(txtParams, appSecret));

            // 是否需要上传文件
            string response;

            response = WebUtils.DoPost(this.serverUrl, txtParams);


            if (FORMAT_XML.Equals(format))
            {
                response = Regex.Replace(response, @"[\x00-\x08\x0b-\x0c\x0e-\x1f]", "");
            }
            //TryParseException(response);
            return response;
        }

        #endregion
    }
}